//
//  ChooseDiseaseDetails.m
//  QuickCure1
//
//  Created by 冬冬 on 2016/11/1.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "ChooseDiseaseDetailsView.h"
#import "NetWorkTool.h"
#import "DoctorApplyModel.h"
#import "ProgressManager.h"

@interface ChooseDiseaseDetailsView ()
@property (nonatomic, strong) DoctorApplyModel *model;
@end

@implementation ChooseDiseaseDetailsView

//点击了已确症按钮
- (IBAction)clickDiagnosisBtn:(UIButton *)sender {
    
    sender.selected = YES;
    self.SuspectedSymptomsBtn.selected = NO;
    self.DiagnosisLbl.text = @"已确诊";
    self.DiagnosisLbl.textColor = [UIColor blackColor];
    if ([self isBtnNormal]) {
        self.treatmentAppliBtn.enabled =YES;
        self.treatmentAppliBtn.backgroundColor = [UIColor blueColor];
    }
}

//点击了症状疑似按钮
- (IBAction)clickSuspectedSymptomsBtn:(UIButton *)sender {
    sender.selected = YES;
    self.DiagnosisBtn.selected = NO;
    self.DiagnosisLbl.text = @"症状疑似";
    self.DiagnosisLbl.textColor = [UIColor blackColor];
    if ([self isBtnNormal]) {
        self.treatmentAppliBtn.enabled =YES;
        self.treatmentAppliBtn.backgroundColor = [UIColor blueColor];
    }
}

//点击了接受过按钮
- (IBAction)clickReceivedBtn:(UIButton *)sender {
    sender.selected = YES;
    
    //就诊申请
    self.treatmentAppliBtn.enabled = NO;
    self.treatmentAppliBtn.backgroundColor = [UIColor lightGrayColor];
    
    self.UnreceivedBtn.selected = NO;
    self.TreatmentModalityView.hidden = NO;
    self.TreatmentLbl.text = @"接受过";
    self.TreatmentLbl.textColor = [UIColor blackColor];
    if ([self isBtnNormal]) {
        self.treatmentAppliBtn.enabled =YES;
        self.treatmentAppliBtn.backgroundColor = [UIColor blueColor];
    }
}

//点击了未接受按钮
- (IBAction)clickUnreceivedBtn:(UIButton *)sender {
    sender.selected = YES;
    self.ReceivedBtn.selected = NO;
    self.TreatmentModalityView.hidden = YES;
    self.TreatmentLbl.text = @"未接受过";
    self.TreatmentLbl.textColor = [UIColor blackColor];
    
    if ([self isBtnNormal]) {
        self.treatmentAppliBtn.enabled =YES;
        self.treatmentAppliBtn.backgroundColor = [UIColor blueColor];
    }
}

//点击了就诊申请
- (IBAction)clickTreatmentAppli:(UIButton *)sender {
    NSLog(@"点击了就诊申请");
    //http://iosapi.itcast.cn/matchedDoctorCount.json.php
    //获取所有医生的信息
    [self accessWebForAllDoctorInfo];
   
}

-(void)awakeFromNib{
    [super awakeFromNib];
   
//    [self.treatmentAppliBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    //根据申请btn的状态给定btn的背景颜色
    if (!self.treatmentAppliBtn.enabled) {
        self.treatmentAppliBtn.backgroundColor = [UIColor lightGrayColor];
    }

    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTap:)];
     UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTap:)];
     UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTap:)];
    //疾病细分
    /*
     //请选择疾病细分lbl
     @property (weak, nonatomic) IBOutlet UILabel *DiseaseSubdivisionLbl;
     
     //请选择并发症lbl
     @property (weak, nonatomic) IBOutlet UILabel *ComplicationLbl;
     */
    self.DiseaseSubdivisionLbl.userInteractionEnabled = YES;
    self.ComplicationLbl.userInteractionEnabled = YES;
    self.ChooseTreatmentLbl.userInteractionEnabled = YES;
    [self.DiseaseSubdivisionLbl addGestureRecognizer:tap1];
    [self.ComplicationLbl addGestureRecognizer:tap2];
    [self.ChooseTreatmentLbl addGestureRecognizer:tap3];
    
    
}

//轻拍
-(void)clickTap:(UITapGestureRecognizer *)sender{
    NSLog(@"轻拍");
    
    if ([self isBtnNormal]) {
        self.treatmentAppliBtn.enabled =YES;
        self.treatmentAppliBtn.backgroundColor = [UIColor blueColor];
    }
    
    //治疗方式lbl
    if (sender.view==self.ChooseTreatmentLbl) {
        if (_receiveType) {
            _receiveType();
        }
    }else if (sender.view==self.ComplicationLbl){//选择并发症
        NSLog(@"并发症");
        
        if (_complicationBlock) {
            _complicationBlock();
        }
        
    }else if (sender.view ==self.DiseaseSubdivisionLbl){//请选择疾病细分
        NSLog(@"疾病细分");
        if (_DiseaseSubdivisionBlock) {
            _DiseaseSubdivisionBlock();
        }
    }
    
}

#pragma mark - 判断btn的状态是否是normal状态
-(BOOL)isBtnNormal{
    if (self.DiseaseSubdivisionLbl.textColor==[UIColor blackColor]&&self.ComplicationLbl.textColor==[UIColor blackColor]&&self.DiagnosisLbl.textColor==[UIColor blackColor]){
        if (self.UnreceivedBtn.selected==YES&&self.TreatmentLbl.textColor==[UIColor blackColor]) {
            [self accessWebForDoctorCount];
            return YES;
        }
        if (self.ReceivedBtn.selected==YES&&self.ChooseTreatmentLbl.textColor==[UIColor blackColor]&&self.TreatmentLbl.textColor==[UIColor blackColor]) {
            [self accessWebForDoctorCount];
            return YES;
        }
    }
    return NO;
}
    
//访问网络得到可咨询的医生数量
-(void)accessWebForDoctorCount{
    
    
    NSString *urlString = kMatchedDoctorCountUrl;
    NSDictionary *paramDic = @{@"ci1_id":@1,
                               @"ci2_id":@3,
                               @"ci3_id":@3,
                               @"diagnosis_type":@0,
                               @"is_confirmed":@1,
                               @"user_id":@1000089,
                               @"has_diagnosis":@2
                               };
    
    //    NSDictionary *parmas = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"is_confirmed":@1,@"user_id":@1000089, @"has_diagnosis":@2};
    __weak ChooseDiseaseDetailsView *weakSelf = self;
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(NSDictionary* responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary *dataDict = responseObject[@"data"];
        NSInteger num = [dataDict[@"doctor_count"] integerValue];
        weakSelf.numLbl.text=[NSString stringWithFormat:@"%zd位",num];
        
    } failureBlock:^(NSError *error) {
        
    }];
    
}
        

//访问网络得到所有医生信息
-(void)accessWebForAllDoctorInfo{
    NSString *urlString = kMatchDoctors;
    NSDictionary *paramDic = @{@"ci1_id":@1,
                               @"ci2_id":@3,
                               @"ci3_id":@3,
                               @"diagnosis_type":@0,
                               @"page_size":@15,
                               @"is_confirmed":@1,
                               @"user_id":@1000089,
                               @"page":@1,
                               @"has_diagnosis":@2
                               };
    
    __weak ChooseDiseaseDetailsView *weakSelf = self;
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary *tempDict = responseObject;
        if([tempDict[@"msg"] isEqualToString:@"OK"]){
            NSArray *dataArr = tempDict[@"data"];
            NSInteger index = arc4random_uniform(15);
            NSDictionary *firstItem = dataArr[index];
            
            weakSelf.model = [[DoctorApplyModel alloc]init];
            [weakSelf.model yy_modelSetWithJSON:firstItem];
            
            //得到本地用户信息
            [weakSelf getLocalUserInfo];
            [[UserManager sharedManager].famousDoctorModelList addObject:weakSelf.model];
            [[UserManager sharedManager] saveFamousDoctorModelList];
//            NSLog(@"%@",self.model);
            [ProgressManager alertSuccessStatus:@"申请成功"];
            
            //清空选择
            [weakSelf appliedEmptyChoice];
        }
    } failureBlock:^(NSError *error) {
        
    }];
    
    
    
}

//得到本地用户信息
-(void)getLocalUserInfo{
    
    //得到当前时间
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *applyTime = [formatter stringFromDate:date];
    
    self.model.applyTime = applyTime;
    //是否审核
    NSInteger examineState = arc4random_uniform(4);
    self.model.isExamine = examineState;
    
    //病人名字
    self.model.patientName = [UserManager sharedManager].userInfo.true_name;
    
    //症状
    self.model.symptom = self.ComplicationLbl.text;
    
    
    
}

//点击就诊申请完后,清空选择
/*
 
 //请选择疾病细分lbl
 @property (weak, nonatomic) IBOutlet UILabel *DiseaseSubdivisionLbl;
 
 //请选择并发症lbl
 @property (weak, nonatomic) IBOutlet UILabel *ComplicationLbl;
 
 //是否确诊lbl
 @property (weak, nonatomic) IBOutlet UILabel *DiagnosisLbl;
 
 //是否接受治疗Lbl
 @property (weak, nonatomic) IBOutlet UILabel *TreatmentLbl;
 //已确症btn
 @property (weak, nonatomic) IBOutlet UIButton *DiagnosisBtn;
 
 //症状疑似Btn
 @property (weak, nonatomic) IBOutlet UIButton *SuspectedSymptomsBtn;
 
 //接受过Btn
 @property (weak, nonatomic) IBOutlet UIButton *ReceivedBtn;
 
 //未接受Btn
 @property (weak, nonatomic) IBOutlet UIButton *UnreceivedBtn;
 
 //治疗方式View
 @property (weak, nonatomic) IBOutlet UIView *TreatmentModalityView;
 
 //选择治疗方式Lbl
 @property (weak, nonatomic) IBOutlet UILabel *ChooseTreatmentLbl;
 */
-(void)appliedEmptyChoice{
    self.DiseaseSubdivisionLbl.text = @"请选择疾病细分";
    self.DiseaseSubdivisionLbl.textColor = [UIColor lightGrayColor];
    self.ComplicationLbl.text = @"请选择并发症(可多选)";
    self.ComplicationLbl.textColor = [UIColor lightGrayColor];
    self.DiagnosisLbl.text = @"是否确诊";
    self.DiagnosisLbl.textColor = [UIColor lightGrayColor];
    self.TreatmentLbl.text = @"是否接受治疗";
    self.TreatmentLbl.textColor = [UIColor lightGrayColor];
    self.numLbl.text = @"0位";
    self.DiagnosisBtn.selected = NO;
    self.SuspectedSymptomsBtn.selected = NO;
    self.ReceivedBtn.selected = NO;
    self.UnreceivedBtn.selected = NO;
    self.TreatmentModalityView.hidden = YES;
    self.ChooseTreatmentLbl.text = @"请选择接受治疗方式";
    self.ChooseTreatmentLbl.textColor = [UIColor lightGrayColor];
    self.treatmentAppliBtn.enabled = NO;
    self.treatmentAppliBtn.backgroundColor = [UIColor lightGrayColor];
}

@end




